package com.chaber.containers.presenters;

import javafx.geometry.Pos;
import javafx.scene.control.Label;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;

import javax.annotation.PostConstruct;
import javax.validation.ConstraintViolation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.util.StringUtils;

import com.chaber.containers.beans.AbstractObjectData;
import com.chaber.containers.common.ConstraintViolationEvent;

public abstract class AbstractPresenter {

	@Autowired
	protected ApplicationContext applicationContext;

	public abstract void bindViewActions();

	protected <T extends AbstractObjectData> void prepareValidationErrors(ConstraintViolationEvent<T> violationEvent, Pane errorsBox) {
		errorsBox.getChildren().clear();

		if (violationEvent.getConstraintViolations() != null) {
			for (ConstraintViolation<T> violation : violationEvent.getConstraintViolations()) {
				Label errorLabel  = this.createErrorLabel();
				errorLabel.setText(violation.getPropertyPath().toString().toUpperCase() + " " + violation.getMessage().toUpperCase());
				
				errorsBox.getChildren().add(errorLabel);
			}
		}

		if (!StringUtils.isEmpty(violationEvent.getMessage())) {
			Label errorLabel  = this.createErrorLabel();
			errorLabel.setText(violationEvent.getMessage().toUpperCase());

			errorsBox.getChildren().add(errorLabel);
		}
	}
	
	private Label createErrorLabel() {
		Label errorLabel = new Label();
		errorLabel.setStyle("-fx-font-size: 14px;-fx-font-weight: bold;color: red;");
		errorLabel.setTextFill(Color.RED);
		errorLabel.setAlignment(Pos.TOP_LEFT);
		
		return errorLabel;
	}

	@PostConstruct
	public void postConstruct() {
		this.bindViewActions();
	}
}
